package com.ysy.logistics.service.system.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysy.logistics.entities.system.RoleNotice;
import com.ysy.logistics.mapper.system.RoleNoticeMapper;
import com.ysy.logistics.myenum.NoticeType;
import com.ysy.logistics.service.system.RoleNoticeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 姚斯羽
 * @date 2025/4/17 21:39
 * @description:
 */
@Service
public class RoleNoticeServiceImpl extends ServiceImpl<RoleNoticeMapper, RoleNotice> implements RoleNoticeService {

    @Resource
    private RoleNoticeMapper roleNoticeMapper;

    @Override
   public List<RoleNotice> getByRoleId(String roleId) {
        LambdaQueryWrapper<RoleNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleNotice::getRoleId,roleId);
        return roleNoticeMapper.selectList(queryWrapper);
    }

    @Override
    public boolean addRoleNotice(String roleId, Integer[] noticeCodes) {
        if (StringUtils.isEmpty(roleId)){
            return false;
        }
        //先根据roleId删除
        LambdaUpdateWrapper <RoleNotice> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(RoleNotice::getRoleId,roleId);
        roleNoticeMapper.delete(updateWrapper);
        //构建列表
        List<RoleNotice> roleNoticeList =new ArrayList<>();
        for (Integer noticeCode : noticeCodes) {
            if (!NoticeType.isExist(noticeCode)) {
                continue;
            }
            RoleNotice roleNotice = new RoleNotice();
            roleNotice.setRoleId(roleId);
            roleNotice.setNoticeCode(noticeCode);
            roleNoticeList.add(roleNotice);
        }
        if (CollectionUtil.isEmpty(roleNoticeList)){
            return false;
        }
        roleNoticeMapper.insert(roleNoticeList);
        return true;
    }

}
